1. /* sdfsqrt.cpp by K.Tsuru */
  2. // function ID 3007 DRADIX
  3. /***********************************
  4. SDouble class
  5. It provides the square root sqrt(x).
  6. ************************************/
  7. #ifndef SN_H
  8. #include "sn.h"
  9. #endif
  10. static const char* func = "Sqrt";
  11. SDouble Sqrt(const SDouble& x){
  12. if( !x.Sign(3007) ) return 0.0;
  13. if( x.Sign() < 0 ) x.SetError(x.DOMAIN_ERR, func, 3007);
  14. SDouble y, rs, d;
  15. //It checks whether an exact value can be obtained by double or not.
  16. //It costs a little time bacause the "dsqrt" is a short number.
  17. uint xfig = x.Last() - x.First();
  18. if(xfig < 4){
  19. SDouble dsqrt;
  20. y.SNError(); //reset error
  21. dsqrt = sqrt( doubleD(x, 0) );
  22. if(y.SNError()==y.NO_ERR){ //detect an error in doubleD()
  23. y = x - dsqrt*dsqrt;
  24. if(y.Sign(3007)== 0) return dsqrt;
  25. }
  26. }
  27. // sqrt(x) = x*(1/sqrt(x))
  28. rs = RecSqrt(x); // = 1/sqrt(x)
  29. y = x*rs;
  30. /***************************************************************
  31. It adds a correction.In some cases it rases up the precision
  32. by about ten percent.It will be confirmed by comparing the two values
  33. Sqrt(Pi()) in the effective figures 120 and 130.
  34. y' = sqrt(x-d) = sqrt(x)*sqrt(1-d/x)
  35. sqrt(x) = y'/sqrt(1-d/x) ~= y'(1+0.5*d/x) = y' + 0.5*d*y'/x
  36. ~= y'+0.5*d*(1/sqrt(x)) = y' + 0.5*d*rs
  37. *****************************************************************/
  38. #if 1
  39. if(y.PreferSpeed() == OFF){
  40. d = x - y*y; // "d" has a few figures.
  41. if(d.Sign()) y += DsDiv(d, 2)*rs;
  42. }
  43. #endif
  44. if(y.Verify()){ // check |x - y^2| << x ?
  45. d = x - y*y;
  46. if(!d.IsMLT(x)){ //It sees the relative error. d<<x?
  47. y.SetError(y.VERIFY, func, 3007);
  48. }
  49. }
  50. return y;
  51. }

sdfsqrt.cpp : last modifiled at 2007/11/01 13:31:27(1,659 bytes)
created at 2017/10/07 10:22:50
The creation time of this html file is 2017/10/07 11:29:39 (Sat Oct 07 11:29:39 2017).